//index.js
//获取应用实例
const app = getApp()

Page({
	data: {
		num: 1,
		data: [],
		http: getApp().globalData.map,
		uid: '',
		index: 0,
		state:false
	},
	index(e) {
		this.setData({
			index: e.currentTarget.dataset.id,
			data:[],
			pg:1
		})
	},
	list(e) {
		this.setData({
			data: [],
			index: e.detail.current
		})
			this.dp()
	},
	onShow() {
		wx.getStorage({
			key: 'uid',
			success: (e) => {
				this.setData({
					uid: e.data,
					num:1,
					data:[]
				})
				this.dp()
			}
		})
	},
	none(e){
		this.setData({
			state:false
		})
	},
	compile(e){
		this.setData({
			state:false
		})
		wx.navigateTo({
			url:'add/add?id='+e.currentTarget.dataset.id
		})
	},
	state(e){
		this.setData({
			state:true, 
			index1:e.currentTarget.dataset.index
		})
	},
	dp() {
		let a=this.data.data
		wx.request({ 
			url: getApp().globalData.http1 + 'action=getGoods&uid='+this.data.uid.uid+'&pg='+this.data.num+'&sta='+this.data.index,
			success:(e)=>{
				console.log(e.data.data)
				let pd=1
				if(e.data.data.length<15){
					pd=0
				}
				e.data.data.forEach(function(item,index){
					a.push(item)
				})
				this.setData({
					data:a,
					pd:pd
				})
			}
		})
	},
	ovo() {
		if (this.data.pd == 1) {
			this.setData({
				pg: this.data.pg + 1
			})
			this.dp()
		}
	},
	putaway(e){
		wx.request({
			url:getApp().globalData.http1+'action=pushGood&gid='+e.currentTarget.dataset.id+'&uid='+this.data.uid.uid,
			success: (e) => {
				console.log(e)
				if(e.data.code==0){
					wx.showToast({
						icon:'none',
						title:e.data.msg
					})
				}else{
					this.setData({
						data:[],
						num:1,
					})
					this.dp()
				}
			}
		})
		this.setData({
			state:false
		})
	},
	sold(e){
		wx.request({
			url:getApp().globalData.http1+'action=downGoods&gid='+e.currentTarget.dataset.id+'&uid='+this.data.uid.uid,
			success: (e) => {
				console.log(e)
				this.setData({
					data:[],
					num:1
				})
				this.dp()
			}
		})
		this.setData({
			state:false
		})
	},
	cancel(e){
		wx.request({
			url:getApp().globalData.http1+'action=delGoods&gid='+e.currentTarget.dataset.id+'&uid='+this.data.uid.uid,
			success: (e) => {
				console.log(e)
				this.setData({
					data:[],
					num:1
				})
				this.dp()
			}
		})
		this.setData({
			state:false
		})
	}
})
